#include <iostream>
#include <functional>
#include <memory>
#include <string>
#include "Comm.hpp"
#include "EpollServer.hpp"
#include "TcpSocket.hpp"
#include "Connection.hpp"
#include "IOHandler.hpp"
using namespace std;
using namespace NS_Connection;



int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        cout << "Usage: " << argv[0] << " port" << endl;
        return 1;
    }

    // 创建Reactor对象
    unique_ptr<EpollServer> svr = make_unique<EpollServer>();
    svr->Init();

    // 创建监听套接字
    uint16_t port = stoi(argv[1]);
    unique_ptr<TcpSocket> listensock = make_unique<TcpSocket>();
    listensock->ListenSocket(port);
    LOG(Debug, "创建listensock成功\n");

    // 将listensock设置为非阻塞
    SetNonBlock(listensock->GetSockFd());
    LOG(Debug, "创建非阻塞成功\n");

    // 创建连接对象
    Connection* conn = Factory::BuildListenConn(listensock->GetSockFd(), EPOLLIN | EPOLLET, svr.get(), Accept);
    LOG(Debug, "创建Connection成功\n");

    // 将连接对象添加到Reactor中
    svr->AddConnection(conn);
    LOG(Debug, "添加Connection对象成功\n");

    svr->Dispatch();

    return 0;
}